//
//  ViewController.m
//  UI02_homework
//
//  Created by Hanlingyu on 16/7/15.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"
#import "RegisterViewController.h"
#import "ForgetKeyPassViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height



@interface ViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *logoImageView;
@property (nonatomic, strong) UITextField *userNameTextField;
@property (nonatomic, strong) UITextField *keyTextField;
@property (nonatomic, strong) UIView *textFieldBgView;
@property (nonatomic, strong) UIView *textFieldWhiteLineView;
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UIImageView *keyPassImageView;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *forgetKeyPassButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UILabel *protocolLabel;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor redColor];
    
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 25, 25);
    button.center = CGPointMake(WIDTH - 50, 44);
    //    button.backgroundColor = [UIColor clearColor];
    [button setBackgroundImage:[UIImage imageNamed:@"icon_white_close"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view insertSubview:button atIndex:self.view.subviews.count - 1];
//    [self.view addSubview:button];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    
    // 背景图
    self.backgroundImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backgroundImageView.backgroundColor = [UIColor whiteColor];
    self.backgroundImageView.image = [UIImage imageNamed:@"welcome_2.jpg"];
    [self.view addSubview:self.backgroundImageView];
    
    // 显示发白的View
    self.backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backgroundView.backgroundColor = [UIColor lightGrayColor];
    self.backgroundView.alpha = 0.35;
    [self.view addSubview:self.backgroundView];
    
    // logo
    self.logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3 - 10, HEIGHT * 2 / 7)];
    self.logoImageView.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 4);
    self.logoImageView.backgroundColor = [UIColor clearColor];
    self.logoImageView.image = [UIImage imageNamed:@"logo_lagou.jpg"];
    [self.view addSubview:self.logoImageView];
    
    // 用户名和密码输入框的背景View
    self.textFieldBgView = [[UIView alloc] initWithFrame:CGRectMake(10, 370, WIDTH - 20, 91)];
    self.textFieldBgView.backgroundColor = [UIColor blackColor];
    self.textFieldBgView.alpha = 0.7;
    [self.view addSubview:self.textFieldBgView];
    self.textFieldBgView.layer.cornerRadius = 6;
    
    // 用户名输入框
    self.userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(55, 0, WIDTH - 20 - 55, 45)];
    self.userNameTextField.placeholder = @"请输入已验证手机号或邮箱";
    self.userNameTextField.backgroundColor = [UIColor grayColor];
    self.userNameTextField.alpha = 0.3f;
    [self.textFieldBgView addSubview:self.userNameTextField];
    
    // 密码输入框
    self.keyTextField = [[UITextField alloc] initWithFrame:CGRectMake(55, 45, WIDTH - 75, 45)];
    self.keyTextField.placeholder = @"请输入密码";
    self.keyTextField.backgroundColor = [UIColor grayColor];
    self.keyTextField.alpha = 0.3;
    [self.textFieldBgView addSubview:self.keyTextField];
    
    // 间隙
    self.textFieldWhiteLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 46, WIDTH - 20, 1)];
    self.textFieldWhiteLineView.backgroundColor = [UIColor whiteColor];
    self.textFieldWhiteLineView.alpha = 0.5;
    [self.textFieldBgView addSubview:self.textFieldWhiteLineView];
    
    // 用户名图片
    self.userImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 11, 20, 20)];
    self.userImageView.backgroundColor = [UIColor clearColor];
    self.userImageView.image = [UIImage imageNamed:@"icon_username.jpg"];
    [self.textFieldBgView addSubview:self.userImageView];
    
    // 密码图片
    self.keyPassImageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 58, 20, 20)];
    self.keyPassImageView.backgroundColor = [UIColor clearColor];
    self.keyPassImageView.image = [UIImage imageNamed:@"icon_password.jpg"];
    [self.textFieldBgView addSubview:self.keyPassImageView];
    
    // 登录按钮
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton.backgroundColor = [UIColor colorWithRed:0.00f green:0.68f blue:0.53f alpha:1.00f];
    self.loginButton.frame = CGRectMake(10, 520, WIDTH - 20, 50);
    self.loginButton.layer.cornerRadius = 4;
    [self.view addSubview:self.loginButton];
    [self.loginButton setTitle:@"登 录" forState:UIControlStateNormal];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

    
    // 忘记密码按钮
    self.forgetKeyPassButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.forgetKeyPassButton.backgroundColor = [UIColor clearColor];
    self.forgetKeyPassButton.frame = CGRectMake(WIDTH - 70, 462, 60, 40);
    [self.forgetKeyPassButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetKeyPassButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.forgetKeyPassButton];
    [self.forgetKeyPassButton addTarget:self action:@selector(forgetKeyPass) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    // 注册按钮
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registerButton.frame = CGRectMake(10, 0, WIDTH - 20, 50);
    self.registerButton.layer.cornerRadius = 4;
    self.registerButton.center = CGPointMake(WIDTH / 2, 620);
    self.registerButton.layer.borderColor = [UIColor colorWithRed:0.00f green:0.68f blue:0.53f alpha:1.00f].CGColor;
    self.registerButton.layer.borderWidth = 2;
    self.registerButton.backgroundColor = [UIColor colorWithRed:0.57f green:0.87f blue:0.70f alpha:1.00f];
    [self.registerButton setTitle:@"注册拉钩" forState:UIControlStateNormal];
    [self.registerButton setTitleColor:[UIColor colorWithRed:0.00f green:0.68f blue:0.53f alpha:1.00f] forState:UIControlStateNormal];
    [self.view addSubview:self.registerButton];
    [self.registerButton addTarget:self action:@selector(registerUser) forControlEvents:                UIControlEventTouchUpInside];
    
    
    self.protocolLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    self.protocolLabel.center = CGPointMake(WIDTH / 2, 482);
    self.protocolLabel.text = @"注册代表你已经同意拉钩用户协议";
    self.protocolLabel.textAlignment = NSTextAlignmentCenter;
    self.protocolLabel.textColor = [UIColor whiteColor];
    self.protocolLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.protocolLabel];
    self.protocolLabel.hidden = YES;
    
    
    

    
    
    // 监听键盘的弹起和回收
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldShouldBeginEditing:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldShouldEndEditing:) name:UIKeyboardWillHideNotification object:nil];
    
    self.userNameTextField.delegate = self;
    self.keyTextField.delegate = self;
    
    
}

- (void)clickAction:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.textFieldBgView.center = CGPointMake(WIDTH / 2, 200);
        self.logoImageView.alpha = 0;
        self.loginButton.transform = CGAffineTransformMakeTranslation(0, -200);
        self.forgetKeyPassButton.transform = CGAffineTransformMakeTranslation(0, -200);
        self.protocolLabel.transform = CGAffineTransformMakeTranslation(0, -200);
        if (self.protocolLabel.hidden == NO) {
            self.protocolLabel.hidden = NO;
        } else {
            self.protocolLabel.hidden = YES;
        }
    }];

    

    if (textField == self.userNameTextField) {
        self.userImageView.image = [UIImage imageNamed:@"icon_username_select.jpg"];
        self.keyPassImageView.image = [UIImage imageNamed:@"icon_password.jpg"];

        NSLog(@"username开启高亮");

    } else if (textField == self.keyTextField){
        self.keyPassImageView.image = [UIImage imageNamed:@"icon_password_select.jpg"];
        self.userImageView.image = [UIImage imageNamed:@"icon_username.jpg"];

        NSLog(@"keypass开启高亮");
    }

    return YES;

}
//- (BOOL)


- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.textFieldBgView.center = CGPointMake(WIDTH / 2, 400);
        self.logoImageView.alpha = 1;
        //        self.loginButton.center = CGPointMake(WIDTH / 2, 520);
        self.loginButton.transform = CGAffineTransformIdentity;
        self.forgetKeyPassButton.transform = CGAffineTransformIdentity;
        self.protocolLabel.transform = CGAffineTransformIdentity;
        
        if (self.protocolLabel.hidden == NO) {
            
            self.protocolLabel.hidden = NO;
            
        } else {
            self.protocolLabel.hidden = YES;
        }
        
    }];
    


    if (textField == self.keyTextField) {
        self.userImageView.image = [UIImage imageNamed:@"icon_username.jpg"];
        self.keyPassImageView.image = [UIImage imageNamed:@"icon_password_select.jpg"];

        NSLog(@"username关闭高亮");
    } else if (textField == self.userNameTextField){
        self.keyPassImageView.image = [UIImage imageNamed:@"icon_password.jpg"];
        self.userImageView.image = [UIImage imageNamed:@"icon_username_select.jpg"];

        NSLog(@"keyPass关闭高亮");
    }


    return YES;

}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
   
    
    
    return YES;
}



- (void)keyboardWillHidden:(NSNotification *)notification {
    
//    NSLog(@"键盘回收");
//    NSLog(@"%@", self.loginButton);
    [UIView animateWithDuration:1 animations:^{
        
        self.textFieldBgView.center = CGPointMake(WIDTH / 2, 400);
        self.logoImageView.alpha = 1;
//        self.loginButton.center = CGPointMake(WIDTH / 2, 520);
        self.loginButton.transform = CGAffineTransformIdentity;
        self.forgetKeyPassButton.transform = CGAffineTransformIdentity;
        self.protocolLabel.transform = CGAffineTransformIdentity;

        if (self.protocolLabel.hidden == NO) {
            
            self.protocolLabel.hidden = NO;
            
        } else {
            self.protocolLabel.hidden = YES;
        }

    }];
    
}


- (void)keyboardWillAppear:(NSNotification *)notification {
    
//    NSLog(@"键盘出现");

    [UIView animateWithDuration:1 animations:^{
        
        self.textFieldBgView.center = CGPointMake(WIDTH / 2, 200);
        self.logoImageView.alpha = 0;
        self.loginButton.transform = CGAffineTransformMakeTranslation(0, -200);
        self.forgetKeyPassButton.transform = CGAffineTransformMakeTranslation(0, -200);
        self.protocolLabel.transform = CGAffineTransformMakeTranslation(0, -200);
        if (self.protocolLabel.hidden == NO) {
            self.protocolLabel.hidden = NO;
        } else {
            self.protocolLabel.hidden = YES;
        }
    }];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    
    if ([self.userNameTextField becomeFirstResponder]) {
        [self.userNameTextField endEditing:YES];
    } else if ([self.keyTextField becomeFirstResponder])
        [self.keyTextField endEditing:YES];
    }


- (void)login {
    
    
    

}

- (void)registerUser {

    [self.registerButton removeTarget:self action:@selector(registerUser) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton addTarget:self action:@selector(returnLogin) forControlEvents:UIControlEventTouchUpInside];

    [self.loginButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerButton setTitle:@"返回登录" forState:UIControlStateNormal];
    
    
    self.userNameTextField.placeholder = @"请输入手机号";
    self.keyTextField.placeholder = @"请输入验证码";
    
    [self.userNameTextField becomeFirstResponder];

    //
    [UIView animateWithDuration:1 animations:^{
        
        self.textFieldBgView.center = CGPointMake(WIDTH / 2, 200);
        self.logoImageView.alpha = 0;
        //        self.loginButton.center = CGPointMake(WIDTH / 2, 300);
        self.loginButton.transform = CGAffineTransformMakeTranslation(0, -200);
        self.protocolLabel.transform = CGAffineTransformMakeTranslation(0, -200);
        
        self.protocolLabel.hidden = NO;
        self.forgetKeyPassButton.hidden = YES;
        
    }];
    
}

- (void)returnLogin {

    
    [self.registerButton removeTarget:self action:@selector(returnLogin) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButton addTarget:self action:@selector(registerUser) forControlEvents:UIControlEventTouchUpInside];

    [self.registerButton setTitle:@"注册拉钩" forState:UIControlStateNormal];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
    self.userNameTextField.placeholder = @"请输入已验证手机号或邮箱";
    self.keyTextField.placeholder = @"请输入密码";
    
    [self.userNameTextField becomeFirstResponder];

    [UIView animateWithDuration:1 animations:^{
        
        self.textFieldBgView.center = CGPointMake(WIDTH / 2, 200);
        self.logoImageView.alpha = 0;
        //        self.loginButton.center = CGPointMake(WIDTH / 2, 300);
        self.loginButton.transform = CGAffineTransformMakeTranslation(0, -200);
        self.protocolLabel.transform = CGAffineTransformMakeTranslation(0, -200);

        self.protocolLabel.hidden = YES;
        self.forgetKeyPassButton.hidden = NO;
        
    }];
}
- (void)forgetKeyPass {
//    ForgetKeyPassViewController *forgetKeyPassVC = [[ForgetKeyPassViewController alloc] init];
//    forgetKeyPassVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
//    [self presentViewController:forgetKeyPassVC animated:YES completion:^{
//    }];
    [self.navigationController pushViewController:[[ForgetKeyPassViewController alloc] init] animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
